package com.example.androidproject.app.deprecated;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.androidproject.R;
import com.example.androidproject.app.adapter.ClassifyAdapter3;
import com.example.androidproject.app.deprecated.temporarily_deprecated_adapter.ClassifyAdapter2;
import com.example.androidproject.utils.GetContent;
import com.example.androidproject.utils.ResultsConstant;
import com.example.androidproject.utils.RequestConstant;
import com.example.androidproject.utils.RvBottomManager;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 弃用
 */

public class ClassifyActivity2 extends Activity {
    private String TAG = "GGSTUDY";
    private OkHttpClient okHttpClient;
    private int classifyFlag;
    private RecyclerView rv_classify;
    private ClassifyAdapter2 adapter;
    private int total = 0;
    private int limit = 15;
    private int page = 1;
//    private LinearLayout ll_contentLoding;

    CountDownLatch latch = new CountDownLatch(2);
    private RvBottomManager rvBottomManager;
    private int COMPLETED = 0;
    private ClassifyAdapter2 classifyAdapter2;

    private int RefreshPositionStart = 0;
    private int itemCount = 0;
    //
    private ImageView back_top_btn;
    private ClassifyAdapter3 classifyAdapter3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_classify2);

        okHttpClient = new OkHttpClient();
        Intent intent = getIntent();
        classifyFlag = intent.getIntExtra("flag", 0);

        getOptions();
        GetContent.getContent("全部", classifyFlag, limit, page, latch);
        initView();
        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        initPage();
    }


    private void initView() {
        rv_classify = findViewById(R.id.rv_classify);
        ResultsConstant.ll_contentLoding = findViewById(R.id.ll_contentLoding);
    }

    private void initPage() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        rv_classify.setLayoutManager(linearLayoutManager);
        classifyAdapter2 = new ClassifyAdapter2(this, classifyFlag);
        rv_classify.setHasFixedSize(true);
//        rv_classify.setItemViewCacheSize(20);
//        rv_classify.setDrawingCacheEnabled(true);
//        rv_classify.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
        rv_classify.setAdapter(classifyAdapter2);
        rvBottomManager = new RvBottomManager(this, rv_classify, new RvBottomManager.Task() {

            @Override
            public void work() {
//                if (classifyFlag == 1) {
//                    RefreshPositionStart = ResultsConstant.MovieList.size();
//                    itemCount = ResultsConstant.contentMovie.size();
//                }
//                if (classifyFlag == 2) {
//                    RefreshPositionStart = ResultsConstant.NovelList.size();
//                    itemCount = ResultsConstant.contentNovel.size();
//                }
                GetContent.rvBottomManager = rvBottomManager;
                page += 1;
                GetContent.getContent("全部", classifyFlag, limit, page, latch);
                Message message = new Message();
                message.what = COMPLETED;
                handler.sendMessage(message);
            }
        }, back_top_btn, classifyAdapter3);
    }

    private Handler handler = new Handler(Looper.getMainLooper()) {
        @SuppressLint("NotifyDataSetChanged")
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == COMPLETED) {
                if (limit * page >= ResultsConstant.total) {
                    rvBottomManager.setEnd(true);
                    Toast.makeText(ClassifyActivity2.this, "数据见底了呦", Toast.LENGTH_SHORT).show();
                }
//                classifyAdapter.notifyItemRangeChanged(RefreshPositionStart, itemCount);
                classifyAdapter2.notifyDataSetChanged();
            }
        }
    };

    // 之所以在copedata里赋值了OptionData initview中却还是null的原因就是 请求是异步的 比较慢所以先完成了initview的
    private void getOptions() {
        String classify = "电影";
        if (classifyFlag == 1) {
            classify = "电影";
        }
        if (classifyFlag == 2) {
            classify = "小说";
        }
        String url = RequestConstant.CLASSIFY_URL + "?" + "classify=" + classify;
        Request request = new Request.Builder().url(url).build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                Log.d(TAG, "onFailure: " + e);
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                if (response.isSuccessful()) {
                    GetContent.copeData(response.body().string(), 1);
                    latch.countDown();
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (ResultsConstant.MovieList != null) ResultsConstant.MovieList.clear();
        if (ResultsConstant.NovelList != null) ResultsConstant.NovelList.clear();
    }
}